package com.avalieme.classificadores;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import weka.core.tokenizers.WordTokenizer;

/**
 * Classe que trata as mensagens (tweets, no caso)
 * limpando-as para melhor padronização.
 * @author Jose Laerte
 * @author Raissa Sarmento
 *
 */
public class TratamentoTweets extends WordTokenizer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 541122714727633804L;

	private static final String[] LISTA_EMOTICONS = { ">:]", ":-)", ":)", ":o)",
			":]", ":3", ":c)", ":>", "=]", "8)", "=)", ":}", ":^", ">=]",
			"=-)", "=P", ":P", ":p", "=3", ":*)", "=*", "=X", ":X", "=B", "=F",
			":F", ">:D", ":-D", ":D", "8-D", "8D", "x-D", "xD", "X-D", "XD",
			"=-D", "=D", "=-3", "=3", ":'(", ";*(", "T.T", "T_T", "Y.Y", "Y_Y",
			">:[", ":-(", ":(", ":-c", ":c", ":-<", ":<", ":-[", ":[", ":{",
			">.>", "<.<", ">.<", ">;]", ";-)", ";)", "*-)", "*)", ";-]", ";]",
			";D", ">:P", ":-P", ":P", "X-P", "x-p", "xp", "XP", ":-p", ":p",
			"=p", ":-*", ":*", ":-b", ":b", ">:\\", ">:/", ":-/", ":-", ":/",
			":\\", "=/", "=\\", ":S", ":|", "=|", "=(", "*-*", ":O", ";o",
			";*", "=*", ";/", ";\\", "=S", "ç.ç", "\\o/", "/o\\", "/o/",
			"\\o\\", "<o>", "o/", "\\o", ":T", "=T", "=t", ":t" };

	private static final String IGNORADO = "<IGNORADO421847824721>";

	public static String[] SUBSTITUIR = { "a", "e", "i", "o", "u", "c", "n" };

	public static Pattern[] PADROES;

	public TratamentoTweets() {
		setDelimiters(" \r\n\t.,'\"?!#"); //delimitadores das mensagens
	}

	public Object nextElement() {
		Object next = super.nextElement();
		if (next instanceof String) {
			return processamento(next);
		}
		return next;
	}
	
	private String removerCaracteresInuteis(String text) {
		return text.replace(":", "").replace(";", "").replace(")", "")
				.replace("(", "").replace("<", "").replace(">", "")
				.replace("{", "").replace("}", "").replace("[", "")
				.replace("]", "");
	}

	private Object processamento(Object next) {
		String text = (String) next;
		text = removerAcentos(text);
		text = removeCaracteresRepetidos(text);
		if (ehEmoticon(text)) {
			return text;
		}
		String cleanText = removerCaracteresInuteis(text);
		if (cleanText.startsWith("@") || cleanText.startsWith("http")
				|| cleanText.equals("")) {
			return IGNORADO;
		}
		return cleanText;
	}

	private String removeCaracteresRepetidos(String text) {
		int end = text.length() - 1;
		while (end >= 1 && text.charAt(end - 1) == text.charAt(end)) {
			text = text.substring(0, end);
			end--;
		}
		return text;
	}

	public String removerAcentos(String text) {
		if(PADROES == null){
			compilarPadroes();
		}
		String result = text;
		for (int i = 0; i < PADROES.length; i++) {
			Matcher matcher = PADROES[i].matcher(result);
			result = matcher.replaceAll(SUBSTITUIR[i]);
		}
		return result;
	}
	
	private String reverse(String text) {
		StringBuilder sb = new StringBuilder(text);
		return sb.reverse().toString();
	}
	
	public void compilarPadroes() {
		PADROES = new Pattern[SUBSTITUIR.length];
		PADROES[0] = Pattern.compile("[âãáàä]", Pattern.CASE_INSENSITIVE);
		PADROES[1] = Pattern.compile("[éèêë]", Pattern.CASE_INSENSITIVE);
		PADROES[2] = Pattern.compile("[íìîï]", Pattern.CASE_INSENSITIVE);
		PADROES[3] = Pattern.compile("[óòôõö]", Pattern.CASE_INSENSITIVE);
		PADROES[4] = Pattern.compile("[úùûü]", Pattern.CASE_INSENSITIVE);
		PADROES[5] = Pattern.compile("[ç]", Pattern.CASE_INSENSITIVE);
		PADROES[6] = Pattern.compile("[ñ]", Pattern.CASE_INSENSITIVE);
	}

	private boolean ehEmoticon(String text) {
		String invertido = reverse(text);
		for (String smile : LISTA_EMOTICONS) {
			if (text.equalsIgnoreCase(smile)
					|| invertido.equalsIgnoreCase(smile)) {
				return true;
			}
		}
		return false;
	}

}
